
package com.idealighter.game.games.lkby.handler;

import com.idealighter.game.core.annotation.ResMsgHandler;
import com.idealighter.game.dictionary.domain.LkpyRoomDomain;
import com.idealighter.game.dictionary.domain.RobotConfigDomain;
import com.idealighter.game.games.lkby.LkbyPlayer;
import com.idealighter.game.games.lkby.message.ResChipsChangeMsg;
import com.idealighter.game.message.ModuleMsgIdConstant;
import com.idealighter.game.message.core.ResMessage;
import com.idealighter.game.robot.core.Player;
import com.idealighter.game.robot.handler.ResMessageHandler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


@ResMsgHandler(ModuleMsgIdConstant.Lkby.RES_CHIPS_CHANGE)
public class ResChipsChangeHandler implements ResMessageHandler {

  private static final Logger LOG = LoggerFactory.getLogger(ResChipsChangeHandler.class);

  @Override
  public void action(Player player, ResMessage message) {
    ResChipsChangeMsg msg = (ResChipsChangeMsg) message;
    if (msg.getPlayerId() != player.playerId) {
      return;
    }

    LkbyPlayer lkbyPlayer = (LkbyPlayer) player;
    RobotConfigDomain robotCfg = lkbyPlayer.configDomain;
    LkpyRoomDomain roomDom = lkbyPlayer.roomDomain;
    long chips = msg.getChips();

    lkbyPlayer.chips = chips;
    // 捕鱼机器人身上的鱼币转化为金币
    long gold = lkbyPlayer.playerInfo.getGold()
        + chips * roomDom.getProportionGold() / roomDom.getProportionChips();

    // 超过最大携带金币需要退出房间
    if (gold > robotCfg.getMaxGold()) {
      LOG.info("[李逵捕鱼]玩家[{}][{}]身上的金币超过最大携带金币", player.userName, player.playerId,
          robotCfg.getMaxGold());
      lkbyPlayer.stopFire();
      player.delayLogout();
      return;
    }

    /*
     * if (gold < roomDom.getLower()) { LOG.info("[李逵捕鱼]机器人[{}][{}]金币小于房间下限退出游戏", player.userName,
     * player.playerId); player.delayLogout(); return; }
     */

    if (gold > roomDom.getUpper()) {
      LOG.info("[李逵捕鱼]机器人[{}][{}]金币高于房间下限退出游戏", player.userName, player.playerId);
      lkbyPlayer.stopFire();
      player.delayLogout();
      return;
    }
  }
}
